// pages/list_detail/list_detail.js
var t = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 跳转数据
    options:'',
    ticketId:"",
    index1:1000,
    index2:1000,
    index3: 1000,
    specificationsState:true,
    // 数据渲染
    ticket:[],
    // 票类型
    piaoList:"",
    stateone:"",
    statetwo:"",
    //票价
    piaoMoney:"0",
    ticketname:"",
    // 分享
    hidden: true,
    // 海报图
    ticketId: '',
    poster: "",
    haibaoshop:true,
    // 商品倒计时
    actEndTimeList:[],
    lingquyouhui: true,
    countDownList:'',
    yongjinShow:true
  },
  // 立即购买
  addres: function () {

    //  判断是否授权
    var memberInfo = wx.getStorageSync('memberInfo')
    // console.log(wx.getStorageSync('memberInfo'))
    if (memberInfo == '' || memberInfo == undefined || memberInfo == null) {
      wx.navigateTo({
        url: '../shouquan/authorization',
      });
      return false;
    }

    var that=this;
    // 获取票类型数据
    var piaoList = that.data.piaoList;
    var stateone = that.data.stateone;
    var statetwo = that.data.statetwo;
    // 获取id
    var index1 = that.data.index1;
    var index2 = that.data.index2;
    var index3 = that.data.index3;
    //商品价格
    var money = that.data.piaoMoney;
    //商品Id
    var ticketId = that.data.ticketId;
    var ticketname = that.data.ticket.name;
    var peopletype = that.data.stateone
    //是否需要收货地址
    var isNeedAddress = that.data.ticket.isNeedAddress
    //是否需要身份证号
    var isNeedIdnumber = that.data.ticket.isNeedIdnumber
    // 如果选择了票类型
    if (piaoList == "" && stateone == "" && statetwo == ""){
      // 打开弹窗
      this.setData({
        specificationsState: false
      })
    } else if (piaoList == ""){
      wx.showToast({
        title: '请选择票种！',
        icon:'none'
      })
    } else if (stateone == "") {
      wx.showToast({
        title: '请选择票类型！',
        icon: 'none'
      })
    } else if (statetwo == "") {
      wx.showToast({
        title: '请选择票取票方式！',
        icon: 'none'
      })
    }else{
      wx.navigateTo({
        url: '../tijiao_record/tijao_record?index1=' + index1 + '&index2=' + index2 + '&index3=' + index3 + '&money=' + money + '&ticketId=' + ticketId + '&peopletype=' + peopletype + '&ticketname=' + ticketname + '&isNeedAddress=' + isNeedAddress + '&isNeedIdnumber=' + isNeedIdnumber,
      })
      // 跳转完成关闭弹窗
      this.setData({
        specificationsState: true
      })
    }
    
  },
  // 合计票价
  sumprick:function(){
    var that=this;
    var index1 = that.data.index1;
    var index2 = that.data.index2;
    var index3 = that.data.index3;
    if (index1 != 1000 && index2 != 1000 && index3 != 1000){
      wx.request({
        url: t.globalData.appletUrl +'/applet/skuVsmoney',
        data: {
          ticket_person_typeId: index2,
          ticket_typeId: index1,
          ticket_methodId: index3,
          ticketId: that.data.ticketId
        },
        success: function (res) {
          that.setData({
            piaoMoney: res.data.data.money
          })
          // console.log(res.data.data.money)
        }
      })
    }else{
      // console.log(1)
    } 
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    console.log(123456789);
    console.log(options.scene);

    // 海报跳转数据
    if(options.scene){
      //海报跳转
      //options 中的 scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
      var scene = decodeURIComponent(options.scene)
      //var query = options.query.dentistId // 3736

      var parentId;

      var ticketId;

      console.log(scene);

        var sceneArr=scene.split(",");

        if(sceneArr.length>0){

          console.log("海报分享的");

          parentId=sceneArr[0];
          
          ticketId=sceneArr[1];

        }else{

          

        }


        

  

       
    }else{

      parentId=options.id;

      ticketId=options.ticketId;

      console.log("转发分享");

    }

    console.log("上级ID："+parentId);

    wx.setStorageSync('parentId', parentId);

    console.log("parentId"+parentId);

    

    console.log("ticketId:"+ticketId);

    this.setData({
      ticketId: ticketId
    });

    wx.showLoading({
      title: '加载中', 
      mask: true
      })
    // 获取商品id,
    if(options.ticketId){

      console.log("为什么有值");
      this.setData({
        options: options,
        ticketId: options.ticketId
      });

    }

    
    //海报分享
  },
  // 活动倒计时
  timeFormat(param) {//小于10的格式化函数
    return param < 10 ? '0' + param : param;
  },
  countDown() {//倒计时函数
    // 获取当前时间，同时得到活动结束时间数组
    let newTime = new Date().getTime();
    let endTimeList = this.data.actEndTimeList;
    let countDownArr = [];
    let obj = null;
    // console.log(endTimeList)
    // 对结束时间进行处理渲染到页面
    endTimeList.forEach(o => {
      let endTime = new Date(o).getTime();
      // 如果活动未结束，对时间进行处理
      if (endTime - newTime > 0) {
        let time = (endTime - newTime) / 1000;
        // 获取天、时、分、秒
        let day = parseInt(time / (60 * 60 * 24));
        let hou = parseInt(time % (60 * 60 * 24) / 3600);
        let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
        let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
        obj = {
          day: this.timeFormat(day),
          hou: this.timeFormat(hou),
          min: this.timeFormat(min),
          sec: this.timeFormat(sec)
        }
        countDownArr.push(obj);
      } else {//活动已结束，全部设置为'00'
        obj = {
          day: '00',
          hou: '00',
          min: '00',
          sec: '00'
        }
        countDownArr.push(obj);
      }
    })
    // 渲染，然后每隔一秒执行一次倒计时函数
    this.setData({ countDownList: countDownArr })
    // console.log(this.data.countDownList)
    setTimeout(this.countDown, 1000);
  },

  xiangqing:function(){
    var that = this
    wx.request({
      url: t.globalData.appletUrl + "/applet/findTicketdetail",
      data: {
        id: that.data.ticketId
      },
      header: {
        "content-type": "application/json"
      },
      success: function (res) {
        console.log(res);
        let list=[]
        list.push(res.data.snapTime)
        that.setData({
          ticket: res.data,
          piaoMoney: res.data.money,
          actEndTimeList: list
        })
        // 调用倒计时
        that.countDown();
      }
    })
  },
  // 选择规则
  specifications:function(){
    this.setData({
      specificationsState:false
    })
  },
  // 关闭规则
  clothes:function(){
    this.setData({
      specificationsState: true
    })
  },
// 返回首页
  home:function(){
    wx.switchTab({
      url: '../index/index',
    })
  },
  // 票
  piao: function (e) {
    var index = e.currentTarget.id
    var data = e.currentTarget.dataset.piao
    this.setData({
      index1: index,
      piaoList:data
    })
    this.sumprick()
  },
  // 类型1
  stateone:function(e){
    var index = e.currentTarget.id
    var data=e.currentTarget.dataset.one
    this.setData({
      index2:index,
      stateone:data
    })
    this.sumprick()
  },
  // 类型2
  statetwo: function (e) {
    var index = e.currentTarget.id;
    var tow = e.currentTarget.dataset.tow;
     console.log(tow)
    console.log(e)
    this.setData({
      index3: index,
      statetwo:tow
    })
    // console.log(this.data.statetwo)
    this.sumprick()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    setTimeout(function(){
      wx.hideLoading()  //showLoading 只能用此语句关闭
    },1000)
     
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {


    

    var memberInfo=wx.getStorageSync("memberInfo");

    console.log(123);

    console.log(memberInfo);

    console.log(456);
    if (memberInfo == '' || memberInfo == undefined || memberInfo == null) {
      wx.navigateTo({
        url: '../shouquan/authorization',
      })
    }
    var memberInfo=wx.getStorageSync("memberInfo");
    if('2'==memberInfo.apply){

        console.log("用户是分销商");

        this.setData({

          yongjinShow:false,

        });

    }



    var that = this;
    var options = that.data.options;
    var list = that.data.actEndTimeList;
    this.xiangqing()
    if(list!=""|| list!=undefined){
      console.log(111)
      //  分享父id
      if (options.id) {
        wx.request({
          url: t.globalData.appletUrl + "/applet/upgradeMember",
          data: {
            parentId:wx.getStorageSync("parentId"),
            memberId: wx.getStorageSync("memberInfo").id
          },
          success(res) {
            console.log(res)
          }
        })
      }
      // options 中的 scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
      // var scene = decodeURIComponent(options.scene)
      // var query = options.query.dentistId // 3736
      //海报分享
      // options 中的 scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
      var scene = decodeURIComponent(options.scene)
      // scene = "e9b00afbb46f02,0da1f0ebd57ace"
      console.log(scene)
      if (scene) {
        //海报分享过来的商品Id

        //升级会员
        var memberId = scene.split(",")[0];

        console.log("分享过来的会员ID："+memberId)
        
        wx.request({
          url: t.globalData.appletUrl + "/applet/upgradeMember",
          data: {
            parentId: wx.getStorageSync("parentId"),
            memberId: wx.getStorageSync("memberInfo").id
          },
          success(res) {
            console.log(res)
          }
        })
      }else{

        console.log("怎么回事");

      }
      // 海报数据
      this.haibao()
    }
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  // 优惠券关闭
  youguanbi: function () {
    this.setData({
      lingquyouhui: true
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var that = this
    console.log(res)
    return {
      title: "旅游景点",
      path: '/pages/list_detail/list_detail?id=' + wx.getStorageSync("memberInfo").id + "&ticketId=" + that.data.ticketId
    }
    if (res.from == 'menu') {
      var member = wx.getStorageSync("memberId")
      wx.request({
        url: t.globalData.appletUrl + '/applet/memberShare',
        data: {
          shopId: t.globalData.shopId,
          memberId: member
        },
        header: {
          "content-type": "application/json"
        },
        success: function (res) {
           this.setData({
             lingquyouhui: false
           })
        }
      })

      wx.request({
        url: t.globalData.appletUrl + '/applet/forwardCoupon',
        data: {
          shopId: t.globalData.shopId,
          memberId: member
        },
        header: {
          "content-type": "application/json"
        },
        success: function (res) {
          console.log("1212121212121212121212121212121212")
        }
      })
    }
  },
  fenxiang:function(){

    //  判断是否授权
    var memberInfo = wx.getStorageSync('memberInfo')
    // console.log(wx.getStorageSync('memberInfo'))
    if (memberInfo == '' || memberInfo == undefined || memberInfo == null) {
      wx.navigateTo({
        url: '../shouquan/authorization',
      });
      return false;
    }

    if (this.data.hidden == true){
      this.setData({
        hidden: false
      })
    }else{
      this.setData({
        hidden: true
      })
    }
  },
  // 海报数据
  haibao: function () {
    var that = this
    wx.request({
      url:t.globalData.appletUrl + '/applet/shapeTicket',
      data: {
        ticketId: this.data.ticketId,
        memberId: wx.getStorageSync("memberInfo").id
      },
      success(res) {
        console.log(res)
        that.setData({
          poster: t.globalData.appletUrl+'/' +res.data
        })
      }
    })
  },
  // 点击显示生成海报
  dianji: function () {
    var that = this
    this.fenxiang()
    that.setData({
      haibaoshop:false
    })
  },
  // 保存海报图
  savePoster: function () {
    var that = this
    console.log(that.data.poster)
    wx.getSetting({
      success(res) {
        console.log(res)
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success(res) {
              // 本地路径
              wx.getImageInfo({
                src:  that.data.poster,
                success: function (res) {
                  console.log(res)
                  // 保存到手机
                  wx.saveImageToPhotosAlbum({
                    filePath: res.path,
                    success(result) {
                      console.log(result)
                      wx.showToast({
                        title: '海报已保存，快去分享给好友吧。',
                        icon: 'none'
                      })
                      setTimeout(function () {
                        that.setData({
                          haibaoshop:true
                        })
                      }, 1000)
                    }
                  })
                }
              })
            }
          })
        } else {
          wx.getImageInfo({
            src: that.data.poster,// t.globalData.appletUrl + "/" 
            success: function (res) {
              console.log(res)
              // 保存到手机
              wx.saveImageToPhotosAlbum({
                filePath: res.path,
                success(result) {
                  console.log(result)
                  wx.showToast({
                    title: '海报已保存，快去分享给好友吧。',
                    icon: 'none'
                  })
                  setTimeout(function () {
                    that.setData({
                      haibaoshop: true
                    })
                  }, 1000)
                }
              })
            }
          })
        }
      }
    })
  },
  // onShareAppMessage: function (res) {
  //   console.log(wx.getStorageSync("memberInfo").id)
  //   console.log(res)
  //   if (res.from == 'menu') {
  //     var member = wx.getStorageSync("memberId")
  //     wx.request({
  //       url: t.globalData.appletUrl + '/applet/memberShare',
  //       data: {
  //         shopId: t.globalData.shopId,
  //         memberId: member
  //       },
  //       header: {
  //         "content-type": "application/json"
  //       },
  //       success: function (res) {

  //       }
  //     })
  //     wx.request({
  //       url: t.globalData.appletUrl + '/applet/forwardCoupon',
  //       data: {
  //         shopId: t.globalData.shopId,
  //         memberId: member
  //       },
  //       header: {
  //         "content-type": "application/json"
  //       },
  //       success: function (res) {
  //         console.log("1212121212121212121212121212121212")
  //       }
  //     })


  //   }
  //   return {
  //     title: "这个小程序真好",
  //     path: 'pages/list_detail/list_detail?id=' + wx.getStorageSync("memberInfo").id
  //   }
  //   console.log(res + "trrrrrrrrrrrrrrrrrrry")



  // }
})